package com.zhuhjay.msm.controller;

import com.zhuhjay.commonutils.R;
import com.zhuhjay.msm.service.MsmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author ZhuHJay
 * @date 2022/9/8 10:11
 */
@Api(description = "短信服务")
@RestController
@RequestMapping("/msmservice/msm")

public class MsmController {

    @Resource
    private MsmService msmService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "发送短信")
    @GetMapping("send/{phone}")
    public R sendMsm(@PathVariable String phone) {
        // 判断是否已经发送，在有效期内
        String code = redisTemplate.opsForValue().get(phone);
        if (StringUtils.hasLength(code)) {
            return R.error().message("验证码已发送");
        }
        return msmService.send(phone) ? R.ok() : R.error();
    }
}
